package com.example.figurehecheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.figurehecheng.common.constant.MessageConstant;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.exception.AccountExist;
import com.example.figurehecheng.common.result.PageResult;
import com.example.figurehecheng.domain.dto.couponDTO;
import com.example.figurehecheng.domain.entity.Coupon;
import com.example.figurehecheng.domain.entity.Merchant;
import com.example.figurehecheng.domain.vo.CouponVO;
import com.example.figurehecheng.mapper.CouponMapper;
import com.example.figurehecheng.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.figurehecheng.service.IMerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
@RequiredArgsConstructor
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    private final IMerchantService iMerchantService;

    /**
     * 优惠券搜索
     *
     * @param couponName
     * @return
     */
    @Override
    public List<CouponVO> querySearch(String couponName) {
        //根据当前商家id和名称查询优惠券
     Long currentId = BaseContext.getCurrentId();
        List<CouponVO> couponVOS = new ArrayList<>();
        List<Coupon> coupons = lambdaQuery().
                eq(Coupon::getForeignId, currentId)
                .like( Coupon::getCouponName, couponName)
                .list();
        coupons.forEach(item->{couponVOS.add(BeanUtil.copyProperties(item,CouponVO.class));});
        return couponVOS;
    }

    /**
     * 优惠券分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult queryCoupon(Integer pageNum, Integer pageSize) {
        List<CouponVO> couponVOS = new ArrayList<>();
        Page<Coupon> couponPage = Page.of(pageNum, pageSize);
        //获取当前商家id
        Long currentId = BaseContext.getCurrentId();
        Page<Coupon> page = lambdaQuery()
                .eq(Coupon::getForeignId, currentId)
                .page(couponPage);

        page.getRecords().forEach(item->{couponVOS.add(BeanUtil.copyProperties(item,CouponVO.class));});
        PageResult pageResult = PageResult.builder().
                pages(page.getPages()).
                total(page.getTotal()).
                records(couponVOS).build();
        return pageResult;

    }

    /**
     * 新增优惠券
     * @param couponDto
     */
    @Override
    public void addCoupon(couponDTO couponDto) {
        if (!ObjectUtil.isAllNotEmpty(couponDto.getQuota(),couponDto.getNumber(),couponDto.getBeginTime(),couponDto.getOvertime(),couponDto.getCouponName())){
            throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
        }
        if (couponDto.getBeginTime(). isAfter(couponDto.getOvertime())){
           throw new AccountExist(MessageConstant.TIME_ERROR);
        }

        Coupon coupons = lambdaQuery().eq(Coupon::getCouponName, couponDto.getCouponName())
                .eq(Coupon::getForeignId, BaseContext.getCurrentId())
                .one();
        if (ObjectUtil.isNotEmpty(coupons)){
            throw new AccountExist(MessageConstant.COUPON_EXIT);
        }
        Coupon coupon = BeanUtil.copyProperties(couponDto, Coupon.class);
        coupon.setForeignId(BaseContext.getCurrentId().intValue());
        Merchant merchant = iMerchantService.getById(BaseContext.getCurrentId());
        coupon.setType(merchant.getRole());
        save(coupon);
    }

    /**
     * 修改优惠券相关信息
     * @param couponDto
     */
    @Override
    public void updateCoupon(couponDTO couponDto) {
        if (!ObjectUtil.isAllNotEmpty(couponDto.getQuota(),couponDto.getNumber(),couponDto.getBeginTime(),couponDto.getOvertime(),couponDto.getCouponName())){
            throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
        }
        System.out.println(couponDto.getBeginTime().isAfter(couponDto.getOvertime()));
          if (couponDto.getBeginTime().isAfter(couponDto.getOvertime())){
            throw new AccountExist(MessageConstant.TIME_ERROR);
        }

        boolean update = lambdaUpdate()
                .set(Coupon::getQuota, couponDto.getQuota())
                .set(Coupon::getNumber, couponDto.getNumber())
                .set(Coupon::getBeginTime, couponDto.getBeginTime())
                .set(Coupon::getOvertime, couponDto.getOvertime())
                .set(Coupon::getCouponName, couponDto.getCouponName())
                .eq(Coupon::getForeignId, BaseContext.getCurrentId())
                .eq(Coupon::getId, couponDto.getId())
                .update();
    }

    /**
     * 删除优惠券
     * @param id
     */
    @Override
    public void deleteCoupon(Integer id) {
        remove(new QueryWrapper<Coupon>().lambda().eq(Coupon::getId, id)
                .eq(Coupon::getForeignId, BaseContext.getCurrentId()));
    }
}
